package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.pojo.param.BrandAddNewParam;
import cn.tedu.csmall.product.pojo.param.BrandUpdateInfoParam;
import cn.tedu.csmall.product.service.BrandService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/brand/")
@Api(tags = "05.品牌管理模块")
@Validated
public class BrandController {

    @Autowired
    private BrandService service;

    @PostMapping("add-new")
    @ApiOperation("添加品牌")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid BrandAddNewParam brandAddNewParam) {
        service.addNew(brandAddNewParam);
        return JsonResult.success();
    }

    @PostMapping("delete")
    @ApiOperation("根据id删除品牌")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "brandId", value = "品牌ID", required = true, dataType = "long")
    public JsonResult delete(@Range(min = 1, message = "品牌ID不合法！")
                             @RequestParam Long brandId) {
        service.deleteById(brandId);
        return JsonResult.success();
    }

    @PostMapping("updateInfoById")
    @ApiOperation("更改品牌详情信息")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "品牌ID", required = true, dataType = "long")
    })
    public JsonResult updateInfoById(@RequestParam @Range(min = 1, message = "请提交有效的品牌id") Long id,
                                     @Valid BrandUpdateInfoParam brandUpdateInfoParam) {
        service.updateInfoById(id,brandUpdateInfoParam);
        return JsonResult.success();
    }

    @GetMapping("list")
    @ApiOperation("分页查询品牌列表")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "int")
    public JsonResult list(@RequestParam @Range(min = 1, message = "查询品牌列表失败，请提供正确的页码值！") Integer pageNum) {
        return JsonResult.success(service.list(pageNum));
    }

    @GetMapping("listAllBrand")
    @ApiOperation("查询所有品牌列表")
    @ApiOperationSupport(order = 401)
    public JsonResult listAll() {
        Integer pageNum = 1;
        Integer pageSize = Integer.MAX_VALUE;
        return JsonResult.success(service.list(pageNum, pageSize));
    }

    @GetMapping("listEnableBrand")
    @ApiOperation("查询所有启用的品牌")
    @ApiOperationSupport(order = 402)
    public JsonResult listEnable() {
        return JsonResult.success(service.listEnable());
    }

    @GetMapping("getStandardById")
    @ApiOperation("通过id查询一条数据")
    @ApiOperationSupport(order = 410)
    public JsonResult getStandardById(@RequestParam @Range(min = 1, message = "根据ID查询品牌详情失败，请提供正确的ID值！") Long id) {
        return JsonResult.success(service.getStandardById(id));
    }

    @PostMapping("/{id:[0-9]+}/{enable:[01]}")
    @PreAuthorize("hasAuthority('/pms/brand/update')")
    @ApiOperation("设置启用或者禁用")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "品牌ID", required = true, dataType = "long"),
            @ApiImplicitParam(name = "enable", value = "是否启用", required = true, dataType = "int"),
    })
    public JsonResult setEnableOrDisable(@PathVariable Long id, @PathVariable Integer enable) {
        service.setEnable(id, enable);
        return JsonResult.success();
    }

}
